package com.buss.admin.controller;

import org.jweb.core.bean.PageMode;
import org.jweb.core.query.hibernate.qbc.CriteriaQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.buss.admin.service.ISiteService;
import com.buss.admin.trans.form.SiteForm;
import com.buss.common.entity.HxSite;
import com.buss.common.model.json.EasyUIDataGridResult;
import com.buss.common.model.json.HXResult;
import com.buss.common.util.IDUtils;

@Controller
@RequestMapping("admin/site")
public class SiteController {

	@Autowired
	private ISiteService siteService;

	@RequestMapping("/sitelist")
	@ResponseBody
	public EasyUIDataGridResult getSiteList(SiteForm siteForm) {

		// 分页查询
		CriteriaQuery cq = siteForm.getCriteriaQuery();
		int pageNo = Integer.parseInt(siteForm.getPage());
		int pageSize = Integer.parseInt(siteForm.getRows());
		// 定义返回数据格式
		EasyUIDataGridResult result = new EasyUIDataGridResult();
		PageMode<HxSite> pageMode = siteService.list4page(cq, pageNo, pageSize);

		// 封装数据
		result.setRows(pageMode.getResult());
		result.setTotal(pageMode.getTotal());
		return result;
	}

	@RequestMapping("/addsite")
	@ResponseBody
	public HXResult addSite(HxSite site) {
		long id = IDUtils.getArticleId();
		site.setId(id);
		try {
			siteService.saveOrUpdate(site);
			return HXResult.ok("增加网站信息成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return HXResult.build(500, "服务器出错，添加失败！");
		}

	}

	@RequestMapping("/deletesite")
	@ResponseBody
	public HXResult deleteSite(String ids) {

		String[] id = ids.split(",");
		try {

			for (String i : id) {
				Long longId = new Long(i);
				HxSite hxSite = siteService.get(HxSite.class, longId);
				siteService.delete(hxSite);
			}
			return HXResult.ok("删除信息成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return HXResult.build(500, "服务器出错，删除信息失败！");
		}

	}

	@RequestMapping("/updatesite")
	@ResponseBody
	public HXResult updateSite(HxSite site) {

		try {
			siteService.saveOrUpdate(site);

			return HXResult.ok("修改信息成功！");

		} catch (Exception e) {
			e.printStackTrace();
			return HXResult.build(500, "服务器繁忙，修改信息失败！");
		}

	}

	/*
	 * 管理员相关页面跳转
	 */
	@RequestMapping("/{path}")
	public String goPath(@PathVariable String path) {
		return "admin/site/" + path;
	}
}
